﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BitmapEncoder
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("开始处理");
            ProcessFolder("src");
            Console.WriteLine("处理结束");
        }

        private static void ProcessFolder(string dir)
        {
            foreach (string fileName in Directory.GetFiles(dir, "*.jpg"))
            {
                Process(fileName);
            }
            foreach (string dirName in Directory.GetDirectories(dir))
            {
                ProcessFolder(dirName);
            }
        }

        private static void Process(string fileName)
        {
            FileStream stream;
            byte[] sourceArray = new byte[0];
            byte[] buffer = new byte[0x400];
            using (stream = new FileStream(fileName, FileMode.OpenOrCreate))
            {
                int num2;
                for (long i = stream.Length; i > 0; i -= num2)
                {
                    num2 = (i > 0x400) ? 0x400 : ((int)i);
                    stream.Read(buffer, 0, num2);
                    byte[] destinationArray = new byte[sourceArray.Length + num2];
                    Array.Copy(sourceArray, 0, destinationArray, 0, sourceArray.Length);
                    Array.Copy(buffer, 0, destinationArray, sourceArray.Length, num2);
                    sourceArray = new byte[sourceArray.Length + num2];
                    Array.Copy(destinationArray, 0, sourceArray, 0, sourceArray.Length);
                }
            }
            using (stream = new FileStream(fileName, FileMode.OpenOrCreate))
            {
                stream.WriteByte(1);
                stream.Write(sourceArray, 0, sourceArray.Length);
            }
        }


    }
}
